package com.lx.bbs.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDto;
import com.lx.bbs.dao.TopicMapper;
import com.lx.bbs.entity.BizException;
import com.lx.bbs.entity.Result;
import com.lx.bbs.entity.Topic;
import com.lx.bbs.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * BBS论坛-帖子表-19 前端控制器
 * </p>
 *
 * @author lx
 * @since 2024-11-23
 */
@RestController
@RequestMapping("/topic")
public class TopicController {

    @Autowired
    TopicMapper topicMapper;


    @RequestMapping("queryByBid")
    public Page<Topic> queryByBid(int bid, int page, int rows) {
        Page<Topic> p = new PageDto<>(page, rows);
//        LambdaQueryWrapper<Topic> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(Topic::getBoardid, bid);
//        return topicMapper.selectPage(p, lqw);
        return topicMapper.selectByBid(bid, p);
    }

    @RequestMapping("queryById")
    public Topic queryById(int id) {
        return topicMapper.queryById(id);
    }



    // @RequestBody ==> axios.post("..", 非 URLSearchParams 对象) @RequestBod把json格式的数据转成对象格式
    @RequestMapping("post")
    public Result post(@RequestBody @Valid Topic topic, Errors errors, @SessionAttribute User myinfo) throws BizException {
        if (errors.hasErrors()) {
            throw new BizException("验证错误", errors.getAllErrors());
        }
        topic.setUid(myinfo.getId());
        topicMapper.insert(topic);
        return new Result(1, "发帖成功", null);
    }



}
